package com.ttbj.controller.freight.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.rpc.Result;
import com.ttbj.controller.freight.FreightController;
import com.ttbj.product.api.FreightFacade;
import com.ttbj.product.api.OrderFacade;
import com.ttbj.product.api.WaybillFacade;
import com.ttbj.product.bean.dto.DriverDto;
import com.ttbj.product.bean.dto.FreightDto;
import com.ttbj.product.bean.dto.OrderDto;
import com.ttbj.product.bean.dto.PageGlobal;
import com.ttbj.product.bean.dto.ViewData;
import com.ttbj.product.bean.dto.WaybillDto;
import com.ttbj.util.ResultUtil;
import com.ttbj.util.ViewModelUtil;

@Controller
@RequestMapping("/driver/trade")
public class FreightControllerImpl implements FreightController {
	public static final Logger logger = LoggerFactory.getLogger(FreightControllerImpl.class);

	@Autowired
	private FreightFacade freightFacade = null;

	@Autowired
	private WaybillFacade waybillFacade = null;

	@Autowired
	private OrderFacade orderFacade = null;

	@RequestMapping(value = "/history.do", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public ViewData historyFreight(HttpServletRequest request, String token, Integer isDate) {
		DriverDto driverDto = (DriverDto) request.getAttribute("user");
		List<FreightDto> list = freightFacade.historyFreight(driverDto.getDriverId(), null, isDate);
		return ViewModelUtil.buildSuccess("完成", list);
	}

	@RequestMapping(value = "/list.do", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public ViewData freightList(HttpServletRequest request, String token, Integer isMove, Integer isCompl, PageGlobal page) {
		DriverDto driverDto = (DriverDto) request.getAttribute("user");
		List<FreightDto> list = freightFacade.freightList(driverDto.getDriverId(), isMove, isCompl, page);
		return ViewModelUtil.buildSuccess("完成", list);
	}

	@RequestMapping(value = "/detail.do", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public ViewData detailFreight(HttpServletRequest request, String token, Long waybillId) {
		FreightDto freight = freightFacade.detailFreight(waybillId);
		return ViewModelUtil.buildSuccess("成功", freight);
	}

	@RequestMapping(value = "/notComple.do", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public ViewData notCompleFreight(HttpServletRequest request, String token) {
		DriverDto driverDto = (DriverDto) request.getAttribute("user");
		List<FreightDto> list = freightFacade.historyFreight(driverDto.getDriverId(), false, null);
		return ViewModelUtil.buildSuccess("完成", list);
	}

	@RequestMapping(value = "/recive/order.do", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public ViewData reciveOrder(HttpServletRequest request, String token, Long waybillId) throws Exception {
		return updatefreightState(waybillId, 1);
	}

	@RequestMapping(value = "/recive/goods.do", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public ViewData reciverGoods(HttpServletRequest request, String token, String senderIdcard, Long waybillId) throws Exception {
		return updatefreightState(waybillId, 2);
	}

	@RequestMapping(value = "/deliver/goods.do", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public ViewData deliverGoods(HttpServletRequest request, String token, String senderIdcard, Long waybillId) throws Exception {
		return updatefreightState(waybillId, 3);
	}

	@RequestMapping(value = "/pullback/order.do", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public ViewData pullBackOrder(HttpServletRequest request, String token, Long waybillId) throws Exception {
		if (null == waybillId) {
			ResultUtil.throwExcepion(ResultUtil.createFail("messages", 911, "配送单号"), "配送单号输入错误");
		}
		WaybillDto waybill = waybillFacade.queryWayBill(new WaybillDto(waybillId));
		if (null == waybill) {
			logger.info("配送单号未查询到配送单信息");
			ResultUtil.throwExcepion(ResultUtil.createFail("messages", 911, "配送单号"), "配送单号输入错误");
		}
		waybillFacade.updateState(waybill, -1);

		Long orderId = waybill.getWaybillOrder();

		OrderDto orderDto = orderFacade.queryOrder(orderId);

		if (null == orderDto) { // 订单号未查询订单
			logger.info("出现了错误,配送单中未包含订单信息。订单号：{}", orderId);
			ResultUtil.throwExcepion(ResultUtil.createFail("messages", 911, "配送单号"), "配送单中订单号未查询到订单信息");
		}

		Integer driverAlready = orderDto.getOrderDriverAlready(); // 订单已派的车辆

		if (null == driverAlready || 0 >= driverAlready) { // 已派车辆
			logger.info("出现了错误, 配送单中订单信息未包含已派车辆.已派车辆:{}", driverAlready);
			ResultUtil.throwExcepion(ResultUtil.createFail("messages", 911, "配送单号"), "配送单中订单信息未包含已派车辆或是已派车辆为0");
		}
		orderDto.setOrderDriverAlready(driverAlready - 1);
		orderFacade.updateOrder(orderDto);
		if (0 != orderDto.getOrderAutoComple()) { // 是否已转手动
			waybillFacade.newWaybill(orderDto);
		}
		return ViewModelUtil.buildSuccess("成功");
	}

	private ViewData updatefreightState(Long waybillId, Integer freightState) throws Exception {
		if (null == waybillId) {
			ResultUtil.throwExcepion(ResultUtil.createFail("messages", 911, "配送单号"), "配送单号输入错误");
		}
		WaybillDto waybill = waybillFacade.queryWayBill(new WaybillDto(waybillId));
		if (null == waybill) {
			ResultUtil.throwExcepion(ResultUtil.createFail("messages", 911, "配送单号"), "配送单号未查询到配送单信息");
		}
		waybillFacade.updateState(waybill, freightState);
		return ViewModelUtil.buildSuccess("成功");

	}

}
